package com.cn.demo4.common;

import com.alibaba.fastjson.JSONObject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 图片上传
 */
@Controller
public class WangEdit {
    @RequestMapping("/loadImg/img")
    @ResponseBody
    public void load(HttpServletRequest req, HttpServletResponse resp) throws  IOException {

        //1.乱码
        Tool.setEncoding(req,resp);
        resp.setHeader("Access-Control-Allow-Origin", "http://localhost:63342");
        /* 允许跨域的请求方法GET, POST, HEAD 等 */
        resp.setHeader("Access-Control-Allow-Methods", "*");
        /* 重新预检验跨域的缓存时间 (s) */
        resp.setHeader("Access-Control-Max-Age", "3600");
        /* 允许跨域的请求头 */
        resp.setHeader("Access-Control-Allow-Headers", "*");
        /* 是否携带cookie */
        resp.setHeader("Access-Control-Allow-Credentials", "true");
        resp.setContentType("");
        //2.取得前台的值
        LoadImg.loadImg(req);

        //返回数据
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("url",req.getAttribute("img"));
        resp.getWriter().println(jsonObject);

    }
}
